// -------------------------------------------------------------------------
//    @FileName         :    NFCAccountRedisModule.cpp
//    @Author           :    LvSheng.Huang
//    @Date             :    2016-11-07
//    @Module           :    NFCAccountRedisModule
//    @Desc             :
// -------------------------------------------------------------------------
#include "NFCAccountDBModule.h"

bool NFCAccountDBModule::Init()
{
    m_pKernelModule = pPluginManager->FindModule<NFIKernelModule>();
    m_pLogicClassModule = pPluginManager->FindModule<NFIClassModule>();
    m_pNoSqlModule = pPluginManager->FindModule<NFINoSqlModule>();
    m_pCommonRedisModule = pPluginManager->FindModule<NFICommonDBModule>();
    m_pAsyMysqlModule = pPluginManager->FindModule<NFIAsyMysqlModule>();
    m_pLogModule = pPluginManager->FindModule<NFILogModule>();
    return true;
}

bool NFCAccountDBModule::Shut()
{
    return true;
}

bool NFCAccountDBModule::Execute()
{
    return true;
}

bool NFCAccountDBModule::AfterInit()
{

    return true;
}

int NFCAccountDBModule::VerifyAccount(const std::string& strAccount, const std::string& strPwd)
{
    std::string strAccountKey = m_pCommonRedisModule->GetAccountCacheKey(strAccount);
    NF_SHARE_PTR<NFINoSqlDriver> xNoSqlDriver = m_pNoSqlModule->GetDriverBySuitRandom();
    if (xNoSqlDriver)
    {
        std::string strPassword;
        if (xNoSqlDriver->HGet(strAccountKey, "Password", strPassword) && strPassword == strPwd)
        {
            return 0;
        }
    }

    return 1;
}

bool NFCAccountDBModule::AddAccount(const std::string& strAccount, const std::string& strPwd)
{
    std::string strAccountKey = m_pCommonRedisModule->GetAccountCacheKey(strAccount);
    NF_SHARE_PTR<NFINoSqlDriver> xNoSqlDriver = m_pNoSqlModule->GetDriverBySuitRandom();
    if (xNoSqlDriver)
    {
        return xNoSqlDriver->HSet(strAccountKey, "Password", strPwd);
    }
    return false;
}

bool NFCAccountDBModule::ExistAccount(const std::string& strAccount)
{
    std::string strAccountKey = m_pCommonRedisModule->GetAccountCacheKey(strAccount);
    NF_SHARE_PTR<NFINoSqlDriver> xNoSqlDriver = m_pNoSqlModule->GetDriverBySuitRandom();
    if (xNoSqlDriver)
    {
        return xNoSqlDriver->Exists(strAccountKey);
    }

    return false;
}

bool  NFCAccountDBModule::ExistRoleGuid(const NFGUID& guid)
{
    NF_SHARE_PTR<NFINoSqlDriver> xNoSqlDriver = m_pNoSqlModule->GetDriverBySuitRandom();
    if (xNoSqlDriver)
    {
        return xNoSqlDriver->Exists(guid.ToString());
    }

    return false;
}

int NFCAccountDBModule::SendAsynGetAccountInfoFromMysql(const NFGUID& client, const std::string& account, int gateId, const MYSQL_RETURN_VECKEY_VECVALUE_FUNCTOR& mFunReturnVeckKeyValueRsp)
{
    std::vector<std::string> vecField;
    vecField.push_back(NFrame::AccountInfo::ID());
    vecField.push_back(NFrame::AccountInfo::RoleID());

    NFMsg::AsynMysqlUseData useData;
    useData.set_account(account);
    useData.set_gate_id(gateId);

    std::string strUseData;
    useData.SerializeToString(&strUseData);

    m_pAsyMysqlModule->Query(client, NFrame::AccountInfo::ThisName(), account, vecField, mFunReturnVeckKeyValueRsp, strUseData);
    return 0;
}

void NFCAccountDBModule::RecvAsynGetAccountInfoFromMysql(const std::vector<std::string>& fieldVec, const std::vector<std::string>& valueVec, std::string& account, NFGUID& roleId)
{
    std::string strRoleId;
    for (int i = 0; i < (int)fieldVec.size(); i++)
    {
        if (fieldVec[i] == NFrame::AccountInfo::ID())
        {
            account = valueVec[i];
        }
        else if (fieldVec[i] == NFrame::AccountInfo::RoleID())
        {
            strRoleId = valueVec[i];
        }
    }
    if (account.empty() || strRoleId.empty())
    {
        return;
    }
    roleId.FromString(strRoleId);
}

bool NFCAccountDBModule::CreateAccountInfoToMysql(const std::string& strAccount, const NFGUID& id)
{
    std::vector<std::string> fieldVec;
    std::vector<std::string> valueVec;

    fieldVec.push_back(NFrame::AccountInfo::RoleID());
    valueVec.push_back(id.ToString());

    std::string strUseData;
    m_pAsyMysqlModule->Updata(NFGUID(), NFrame::AccountInfo::ThisName(), strAccount,
                              fieldVec, valueVec, this, &NFCAccountDBModule::RecvCreateAccountInfoToMysql, strUseData);

    return true;
}

void NFCAccountDBModule::RecvCreateAccountInfoToMysql(const NFGUID& self, const int nRet, const std::string& strUseData)
{
    if (nRet == 0)
    {
        m_pLogModule->LogNormal(NFILogModule::NLL_INFO_NORMAL, self, "Save Account Info To Mysql", " Success", __FUNCTION__, __LINE__);
    }
    else
    {
        m_pLogModule->LogNormal(NFILogModule::NLL_ERROR_NORMAL, self, "Save Account Info To Mysql", " Failed", __FUNCTION__, __LINE__);
    }
}

bool NFCAccountDBModule::CreateAccountInfoToRedis(const std::string& strAccount, const NFGUID& id)
{
    std::string strAccountKey = m_pCommonRedisModule->GetAccountCacheKey(strAccount);
    NF_SHARE_PTR<NFINoSqlDriver> xNoSqlDriver = m_pNoSqlModule->GetDriverBySuitRandom();
    if (xNoSqlDriver)
    {
        if (xNoSqlDriver->Exists(strAccountKey) && !xNoSqlDriver->HExists(strAccountKey, NFrame::Player::ID()))
        {
            xNoSqlDriver->HSet(strAccountKey, NFrame::Player::ID(), id.ToString());
            xNoSqlDriver->HSet(id.ToString(), NFrame::Player::ID(), id.ToString());

            return true;
        }
    }

    return false;
}

bool NFCAccountDBModule::LoadAccountInfoFromRedis(const std::string& strAccount, NFGUID& id)
{
    std::string strAccountKey = m_pCommonRedisModule->GetAccountCacheKey(strAccount);
    NF_SHARE_PTR<NFINoSqlDriver> xNoSqlDriver = m_pNoSqlModule->GetDriverBySuitRandom();
    if (xNoSqlDriver)
    {
        if (xNoSqlDriver->Exists(strAccountKey))
        {
            std::string strID;
            bool bRoleIDRet = xNoSqlDriver->HGet(strAccountKey, NFrame::Player::ID(), strID);
            if (bRoleIDRet && !strID.empty())
            {
                return id.FromString(strID);
            }

            return false;
        }
    }

    return false;
}
